//
//  TalkingViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/18.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "TalkingViewController.h"
#import "TalkingView.h"
#import "TalkTableViewCell.h"
#import "TalkInfoViewController.h"
#import <AVOSCloud.h>
@interface TalkingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)TalkingView *talkingView;
@property (nonatomic,strong)NSArray *talkDataArray;
@end

@implementation TalkingViewController
- (void)loadView
{
    self.talkingView = [[TalkingView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.talkingView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.talkingView.backgroundColor = ColorHex(0x0d1c38);
    //取消按钮方法
    [self.talkingView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //注册重用标示符
    [self.talkingView.talkTableView registerClass:[TalkTableViewCell class] forCellReuseIdentifier:@"TalkTableViewCell"];
    //遵守代理
    self.talkingView.talkTableView.delegate = self;
    self.talkingView.talkTableView.dataSource = self;
    //添加手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGrAction)];
    self.talkingView.toolView.userInteractionEnabled = YES;
    [self.talkingView.toolView addGestureRecognizer:tapGR];
    //添加手势
    UIScreenEdgePanGestureRecognizer *edgeGR = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgeGR)];
    [self.talkingView.talkTableView addGestureRecognizer:edgeGR];
    self.talkingView.talkTableView.userInteractionEnabled = YES;
    edgeGR.edges = UIRectEdgeLeft;

}
#pragma mark - 界面刚出现方法
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    AVQuery *query = [AVQuery queryWithClassName:@"Talk"];
    NSString *string = [NSString stringWithFormat:@"talk%ld",self.webID];
    [query whereKeyExists:string];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.talkDataArray = objects;
        [self.talkingView.talkTableView reloadData];
    }];

}
#pragma mark - 手势方法实现
- (void)tapGrAction
{
    TalkInfoViewController *talkInfoVC = [[TalkInfoViewController alloc]init];
    talkInfoVC.passID = self.webID;
    [self presentViewController:talkInfoVC animated:YES completion:nil];
}
#pragma mark - 返回按钮点击事件
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - tableView DataSource代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.talkDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TalkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TalkTableViewCell" forIndexPath:indexPath];
    self.talkingView.talkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    AVUser *current = [AVUser currentUser];
    AVFile *imagefile = [current objectForKey:@"StyleImage"];
    NSData *imageData = [imagefile getData];
    UIImage *headImage = [UIImage imageWithData:imageData];
    NSString *string = [NSString stringWithFormat:@"talk%ld",self.webID];
    AVObject *test = self.talkDataArray[indexPath.row];
    NSString *user = [test objectForKey:@"userName"];
    NSString *textName = [NSString stringWithFormat:@"用户:%@",current.username];
    if (imageData && [textName isEqualToString:user]) {
        cell.headImageView.image = headImage;
    }else{
            cell.headImageView.image = [UIImage imageNamed:@"test.jpeg"];
    }
    if ([textName isEqualToString:user]) {
        if (![[current objectForKey:@"styleNickName"] isEqualToString:@""]) {
            cell.userNickName.text = [current objectForKey:@"styleNickName"];
        }else{
            cell.userNickName.text = [test objectForKey:@"userName"];
        }
    }else{
        cell.userNickName.text = [test objectForKey:@"userName"];
    }
    cell.timeLabel.text = [[LeanCloudService shareService] transTime:test.createdAt];
    cell.talkInfoLabel.text = [test objectForKey:string];
    
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma mark - tableView高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * LAYOUT_SIZE_HEIGHT;
}

#pragma mark - tableView选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - 手势方法实现
- (void)edgeGR
{
    [self dismissViewControllerAnimated:YES completion:nil];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
